﻿Public Class Frm_dalil
    Dim bol As Boolean = True
    Public avo As Annuaire
    Private Sub Btn_ajou_tele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele.Click
        Partager.typePhone = "Tel"
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()

        '------
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_hatif.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_ajou_tele_tabit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele_tabit.Click
        Partager.typePhone = "Fix"
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_fix.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Fix" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_ajou_fax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_fax.Click
        Me.Enabled = False
        Frm_Fax_Bure.txt_tele.Select()
        Frm_Fax_Bure.Show()

        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Fax_Bure.List_tele.Items.Clear()
                Partager.list_fax.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Fax" Then
                        Frm_Fax_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Frm_Rech_dalil.Show()
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        txt_nomComp.Select()
    End Sub

    Private Sub txt_mail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_mail.Click
        If bol = False Then
            txt_mail.Text = ""
        End If
        bol = True
    End Sub

    Private Sub txt_mail_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mail.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_mail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mail.TextChanged
        txt_mail.ForeColor = Color.Black
        txt_mail.TextAlign = HorizontalAlignment.Left
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة شخص جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If txt_mail.Text <> "" Then
                    bol = Partager.regEmail(txt_mail.Text)
                    If (bol = False) Then
                        txt_mail.Text = "البريد الإلكتروني خاطئ"
                        txt_mail.ForeColor = Color.Red
                        txt_mail.TextAlign = HorizontalAlignment.Right
                    End If
                End If

                If (txt_nomComp.Text <> "" And bol = True) Then
                    If MsgBox("هل تريد فعلا إضافة هذا الشخص ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "الإضافة في دليل الهاتف") = MsgBoxResult.Yes Then

                        Try
                            Partager.close()
                            Dim cli As New Annuaire(1, txt_nomComp.Text, txt_adresse.Text, txt_ocup.Text, txt_mail.Text, txt_molahadat.Text)
                            Partager.moi.Ajouter_annuaire(cli)

                            'l'objet en cours
                            Dim lastItem As Integer = Partager.moi.List_annuaire.Count - 1
                            cli = CType(Partager.moi.List_annuaire.Item(lastItem), Annuaire)

                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                            For Each d As Phone In Partager.list_fix
                                cli.Ajouter_phone(d)
                            Next
                            For Each d As Phone In Partager.list_fax
                                cli.Ajouter_phone(d)
                            Next

                            List_tous.Items.Clear()
                            For Each khib As Annuaire In Partager.moi.List_annuaire
                                List_tous.Items.Add(New ListViewItem(New String() {khib.nomComp, khib.occupation, khib.Adress, khib.email}))
                            Next
                            MsgBox("تمت إضافة السشخص بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me)
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        End Try
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            End If
        Catch ex As Exception

            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub Frm_dalil_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Partager.moi.remplir_List_annuaire()
            List_tous.Items.Clear()
            For Each khib As Annuaire In Partager.moi.List_annuaire
                List_tous.Items.Add(New ListViewItem(New String() {khib.nomComp, khib.occupation, khib.Adress, khib.email}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices
                avo = Partager.moi.List_annuaire(i)
                txt_ocup.Text = avo.occupation
                txt_nomComp.Text = avo.nomComp
                txt_adresse.Text = avo.Adress
                txt_mail.Text = avo.email
                txt_molahadat.Text = avo.Remarque

                avo.remplir_List_list_phone()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Partager.list_hatif.Add(numhone)


                    ElseIf numhone.Type = "Fix" Then
                        Partager.list_fix.Add(numhone)


                    ElseIf numhone.Type = "Fax" Then
                        Partager.list_fax.Add(numhone)
                    End If
                Next
            Next
            If List_tous.SelectedIndices.Count <= 0 Then
                Partager.vider(Me)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الشخص ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "الحذف في دليل الهاتف") = MsgBoxResult.Yes Then
                Try
                    Dim clt As Annuaire = Partager.moi.List_annuaire(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_annuaire(avo.Id)

                    List_tous.Items.Clear()
                    For Each khib As Annuaire In Partager.moi.List_annuaire
                        List_tous.Items.Add(New ListViewItem(New String() {khib.nomComp, khib.occupation, khib.Adress, khib.email}))
                    Next
                    MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر شخص من القائمة", MsgBoxStyle.Information, "بحث")
        End If
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If List_tous.SelectedIndices.Count > 0 Then

            If txt_mail.Text <> "" Then
                bol = Partager.regEmail(txt_mail.Text)
                If (bol = False) Then
                    txt_mail.Text = "البريد الإلكتروني خاطئ"
                    txt_mail.ForeColor = Color.Red
                    txt_mail.TextAlign = HorizontalAlignment.Right
                End If
            End If

            If (txt_nomComp.Text <> "" And bol = True) Then
                If MsgBox("هل تريد فعلا تعديل هذا الشخص ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "التعديل في دليل الهاتف") = MsgBoxResult.Yes Then
                    Try
                        Partager.close()
                        Dim cli As New Annuaire(1, txt_nomComp.Text <> "", txt_adresse.Text, txt_ocup.Text, txt_mail.Text, txt_molahadat.Text)
                        Partager.moi.modifier_annuaire(cli, avo.Id)
                        cli.Id = avo.Id
                        If Partager.list_hatif.Count > 0 Then
                            cli.suprimmer_phone(avo.Id, "Tel")
                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                        End If
                        If Partager.list_fix.Count > 0 Then
                            cli.suprimmer_phone(avo.Id, "Fix")
                            For Each d As Phone In Partager.list_fix
                                cli.Ajouter_phone(d)
                            Next
                        End If
                        If Partager.list_fax.Count > 0 Then
                            cli.suprimmer_phone(avo.Id, "Fax")
                            For Each d As Phone In Partager.list_fax
                                cli.Ajouter_phone(d)
                            Next
                        End If
                        List_tous.Items.Clear()
                        For Each khib As Annuaire In Partager.moi.List_annuaire
                            List_tous.Items.Add(New ListViewItem(New String() {khib.nomComp, khib.occupation, khib.Adress, khib.email}))
                        Next
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me)
                    Catch ex As Exception

                        MsgBox(ex.Message)

                    End Try
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
            MsgBox("اختر الشخص من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub
End Class